package city.spring.modules.material.controller;

import city.spring.modules.material.entity.MaterialreceiptapprovalEntity;
import city.spring.modules.material.service.MaterialreceiptapprovalService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.NonNull;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller：领用审核
 *
 * @author 谢金婷
 * @date 2020-06-26 22:39:08
 */
@RestController
@RequestMapping("/receiptapproval")
public class MaterialreceiptapprovalController extends ApiController {
    private final MaterialreceiptapprovalService receiptapprovalService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MaterialreceiptapprovalEntity, ?>> entityOrderFields;

    public MaterialreceiptapprovalController(MaterialreceiptapprovalService receiptapprovalService) {
        this.receiptapprovalService = receiptapprovalService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MaterialreceiptapprovalEntity::getId);
        entityOrderFields.add(MaterialreceiptapprovalEntity::getReceiptId);
        entityOrderFields.add(MaterialreceiptapprovalEntity::getApprovalStatus);
        entityOrderFields.add(MaterialreceiptapprovalEntity::getSuggestion);
        entityOrderFields.add(MaterialreceiptapprovalEntity::getGmtCreate);
        entityOrderFields.add(MaterialreceiptapprovalEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>领用审核</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, MaterialreceiptapprovalEntity entity) {
        LambdaQueryChainWrapper<MaterialreceiptapprovalEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>领用审核</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, MaterialreceiptapprovalEntity entity) {
        LambdaQueryChainWrapper<MaterialreceiptapprovalEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<MaterialreceiptapprovalEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>领用审核</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(receiptapprovalService.getById(id));
    }

    /**
     * 添加一个 <strong>领用审核</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody MaterialreceiptapprovalEntity entity, @NonNull Principal user) {
        receiptapprovalService.saveReceiptapproval(entity, user);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    @PostMapping("/addOcheck")
    public Object addOcheck(@RequestBody MaterialreceiptapprovalEntity entity, @NonNull Principal user) {
        receiptapprovalService.saveReceiptapprovalOcheck(entity, user);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }


    /**
     * 修改一个 <strong>领用审核</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MaterialreceiptapprovalEntity entity) {
        entity.setId(id);
        receiptapprovalService.updateReceiptapproval(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>领用审核</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        receiptapprovalService.deleteReceiptapproval(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>领用审核</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        receiptapprovalService.deleteReceiptapproval(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MaterialreceiptapprovalEntity> buildLambdaQuery(MaterialreceiptapprovalEntity entity) {
        LambdaQueryChainWrapper<MaterialreceiptapprovalEntity> lambdaQuery = receiptapprovalService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), MaterialreceiptapprovalEntity::getId, entity.getId());
        lambdaQuery.eq(entity.getReceiptId() != null, MaterialreceiptapprovalEntity::getReceiptId, entity.getReceiptId());
        lambdaQuery.eq(entity.getApprovalStatus() != null, MaterialreceiptapprovalEntity::getApprovalStatus, entity.getApprovalStatus());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSuggestion()), MaterialreceiptapprovalEntity::getSuggestion, entity.getSuggestion());
        lambdaQuery.eq(entity.getGmtCreate() != null, MaterialreceiptapprovalEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, MaterialreceiptapprovalEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<MaterialreceiptapprovalEntity> buildQuery(MaterialreceiptapprovalEntity entity) {
        QueryWrapper<MaterialreceiptapprovalEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.eq(entity.getReceiptId() != null, "receipt_id", entity.getReceiptId());
        wrapper.eq(entity.getApprovalStatus() != null, "approval_status", entity.getApprovalStatus());
        wrapper.like(StringUtils.isNotBlank(entity.getSuggestion()), "suggestion", entity.getSuggestion());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

    /**
     * 根据领用单查找审核记录
     *
     * @param receiptId
     * @return
     */
    @GetMapping("/getCheckByReceiptId/{receiptId}")
    public Object getCheckByReceiptId(@PathVariable String receiptId) {
        List<MaterialreceiptapprovalEntity> records = receiptapprovalService.getCheckByReceiptId(receiptId);
        return success(records);
    }

}
